/*
 * Copyright 2012 Alibaba.com All right reserved. This software is the
 * confidential and proprietary information of Alibaba.com ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with Alibaba.com.
 */
package com.dj.web.reg.controller;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.dj.core.reg.RegistService;
import com.dj.domain.entity.DaiJiaProfile;
import com.dj.domain.entity.DaiJiaUser;
import com.dj.web.entity.DaijiaWebUser;
import com.dj.web.form.tools.UserFormCopier;
import com.dj.web.utils.DaijiaWebUserUtil;

/**
 * 代驾人附加信息
 * 
 * @author jiansheng.xujs 2012-5-30 下午1:31:16
 */
@Controller
@RequestMapping("/profile")
public class ProfileController {

    @Autowired
    private RegistService registService;

    @RequestMapping(method = RequestMethod.GET, value = "/contact")
    public String face(Model model, HttpServletRequest request) {
        DaijiaWebUser user = DaijiaWebUserUtil.getUserFromRequest(request);
        // 未登录
        if (user == null) {
            return "redirect:/login.htm?done=/profile/contact.htm";
        }
        ContactForm contactForm = new ContactForm();
        if (user != null) {
            contactForm.setEmail(user.getEmail());
            contactForm.setMobile(user.getMobile());
        }
        model.addAttribute(contactForm);
        return "/profile/contact";
    }

    @RequestMapping(method = RequestMethod.POST, value = "/contact")
    public String saveFace(@Valid ContactForm contactForm, BindingResult result, HttpServletRequest request) {
        return "/profile/contact";
    }

    @RequestMapping(method = RequestMethod.GET, value = "")
    public String basic(Model model, HttpServletRequest request) {
        DaijiaWebUser user = DaijiaWebUserUtil.getUserFromRequest(request);
        if (user == null) {
            return "redirect:/login.htm?done=/profile.htm";
        }
        BasicForm basicForm = new BasicForm();
        basicForm.setCity(user.getCity());
        basicForm.setCounty(user.getCounty());
        basicForm.setProvince(user.getProvince());
        basicForm.setName(user.getName());
        basicForm.setSex(user.getSex());
        model.addAttribute(basicForm);
        return "/profile/basic";
    }

    @RequestMapping(method = RequestMethod.POST, value = "")
    public String saveBasic(@Valid BasicForm basicForm, BindingResult result, Model model, HttpServletRequest request) {
        DaijiaWebUser user = DaijiaWebUserUtil.getUserFromRequest(request);
        if (user == null) {
            return "redirect:/login.htm?done=/profile.htm";
        }
        if (result.hasErrors()) {
            return "/profile/basic";
        }
        DaiJiaUser basicUserInfo = UserFormCopier.convertDaijiaUserBasicForm2Do(basicForm, new DaiJiaUser(), user);
        registService.updateBasicInfo(basicUserInfo);
        user.setCity(basicUserInfo.getCity());
        user.setCounty(basicUserInfo.getCounty());
        user.setProvince(basicUserInfo.getProvince());
        user.setName(basicUserInfo.getName());
        user.setSex(basicUserInfo.getSex());
        return "/profile/success";
    }

    @RequestMapping(method = RequestMethod.GET, value = "/daijia")
    public String full(Model model, HttpServletRequest request) {
        DaijiaWebUser user = DaijiaWebUserUtil.getUserFromRequest(request);
        if (user == null) {
            return "redirect:/login.htm?done=/profile/daijia.htm";
        }
        FullProfileForm fullProfileForm = this.getUserProfile(user);
        model.addAttribute(fullProfileForm);
        return "/profile/daijia";
    }

    @RequestMapping(method = RequestMethod.POST, value = "/daijia")
    public String saveFull(@Valid FullProfileForm fullProfileForm, BindingResult result, HttpServletRequest request) {
        if (result.hasErrors()) {
            return "/profile/daijia";
        }
        // 未登录
        if (!DaijiaWebUserUtil.isLogon(request)) {
            request.setAttribute("NEED_LOGIN", "Y");
            return "/profile/daijia";
        }
        DaijiaWebUser webUser = DaijiaWebUserUtil.getUserFromRequest(request);
        DaiJiaProfile profile = UserFormCopier.convertProfileForm2DaijiaProfile(webUser, fullProfileForm,
                                                                                new DaiJiaProfile());
        profile = registService.saveProfile(profile);
        return "/profile/success";
    }

    private FullProfileForm getUserProfile(DaijiaWebUser user) {
        FullProfileForm fullProfileForm = null;
        if (user == null) {
            return new FullProfileForm();
        }
        DaiJiaProfile profileDo = registService.getUserProfile(user.getId());
        fullProfileForm = UserFormCopier.convertDaijiaProfile2ProfileForm(user, profileDo, fullProfileForm);
        if (fullProfileForm == null) {
            fullProfileForm = new FullProfileForm();
            fullProfileForm.setTel(user.getMobile());
            fullProfileForm.setProvince(user.getProvince());
            fullProfileForm.setCity(user.getCity());
            fullProfileForm.setCounty(user.getCounty());
            fullProfileForm.setDrivingLicense("C1");
        }
        return fullProfileForm;
    }

    @RequestMapping(method = RequestMethod.GET, value = "/admin")
    public String admin(Model model, HttpServletRequest request) {
        return "/profile/admin";
    }

    @RequestMapping(method = RequestMethod.POST, value = "/admin")
    public String saveAdmin(Model model, HttpServletRequest request) {
        return "/profile/admin";
    }
}
